<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <crudOperation :permission="permission">
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="danger"
          icon="el-icon-remove-outline"
          :loading="enable"
          @click="crud.enableOrDisable('DISABLE')"
          :disabled="crud.selections.length < 1"
          v-permission="permission.disable"
        >
          禁用
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="success"
          icon="el-icon-circle-check"
          :loading="able"
          :disabled="crud.selections.length < 1"
          @click="crud.enableOrDisable('ENABLE')"
          v-permission="permission.enable"
        >
          启用
        </el-button>
        <el-button
          slot="right"
          class="filter-item"
          size="mini"
          type="success"
          @click="showInit =true"
        >
          初始化
        </el-button>
      </crudOperation>
      <el-dialog  title="初始化"
                  destroy-on-close1
                  v-dialogDrag
                  close-on-press-escape
                  :visible.sync="showInit" append-to-body width="50vw">
        <div style="font-size: 30px;">主表</div>
        <el-select v-model="initPrintQuery.code" value-key="item">
          <el-option
            v-for="(item,index) in dict.t_sys_order_code_model"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>
        <div style="font-size: 20px">明细表&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<span @click="addextra" style="color: blue;font-size: 30px;cursor: pointer">+</span></div>
        <div v-for="item in initPrintQuery.extra">
          <div class="flex flex-row">
            <el-select v-model="item.code" value-key="item">
              <el-option
                v-for="(i,index) in dict.t_sys_order_code_model"
                :key="i.id"
                :label="i.label"
                :value="i.value"
              >
              </el-option>
            </el-select>
            <el-input  v-model="item.columnName"></el-input>
          </div>
        </div>
        <div>
          <el-input
            type="textarea"
            :rows="10"
            placeholder="请输入内容"
            v-model="textarea">
          </el-input>
        </div>
        <div slot="footer" class="dialog-footer">
          <el-button  type="primary" @click="removeextr">重置</el-button>
          <el-button  type="primary" @click="printInitSubmit">确认</el-button>
        </div>
      </el-dialog>
      <div v-if="!crud.props.searchToggle">
        <el-form size="small" label-width="70px">
          <el-row :gutter="10">
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户名称">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="销售部门">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="销售人员">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户分类">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <el-form-item label="客户区域">
                <el-input></el-input>
              </el-form-item>
            </el-col>
            <el-col :xs="24" :sm="12" :md="8" :lg="6" :xl="6">
              <rrOperation :crud="crud"/>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="50%">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="id"  v-show="false">
            <el-input v-model="form.id" style="width: 370px;"/>
          </el-form-item>
          <el-row type="flex" >
            <el-col :span="8">
              <el-form-item label="编码">
                <el-input v-model="form.code" readonly />
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="打印模块">
                <el-select v-model="form.name" value-key="item" @change="upCode">
                  <el-option
                    v-for="(item,index) in dict.t_sys_order_code_model"
                    :key="item.id"
                    :label="item.label"
                    :value="item"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="打印模板">
                <zSelect
                  url="/api/dropDown/printTemplateList"
                  :value.sync="form.templateId"
                >
                </zSelect>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex">
            <el-col :span="8">
              <el-form-item label="部门">
                <zSelect  :value.sync="form.deptName"  url="api/dropDown/childSysDeptList" ></zSelect>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="人员名称">
                <zSelect :value.sync="form.userId"  url="/api/dropDown/sysUserList"
                         @change="updateDept"></zSelect>
              </el-form-item>
            </el-col>
            <el-col :span="9">
              <el-form-item label="排序">
                <el-input v-model="form.seq" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row type="flex">
            <el-col :span="6">
              <el-form-item label="状态">
                <el-switch v-model="switchStatus"
                           active-text="启用"
                           inactive-text="禁用"
                           @change="upStatus"
                >
                </el-switch>
              </el-form-item>
            </el-col>
            <el-col >
              <el-form-item label="备注">
                <el-input v-model="form.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.cu === 2" type="primary" @click="save">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" stripe border v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column :render-header="crud.renderHeader" type="selection" width="60" />
        <el-table-column :render-header="crud.renderHeader" type="index" width="60" label="序号"/>
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('id')" prop="id" label="id" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('code')" prop="code" label="编码" />
<!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('name')" prop="name" label="名称" />-->
        <!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('type')" prop="type" label="模板类型(1:默认)" />-->
        <!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('templateId')" prop="templateId" label="打印模板id" />-->
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('templateName')" prop="templateName" label="打印模板名称" width="200px"/>
        <!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deptId')" prop="deptId" label="部门id" />-->
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deptCode')" prop="deptCode" label="部门编码" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('deptName')" prop="deptName" label="部门名称" />
        <!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userId')" prop="userId" label="人员id" />-->
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userCode')" prop="userCode" label="人员编码" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('userName')" prop="userName" label="人员名称" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('seq')" prop="seq" label="排序" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('status')" prop="status" :formatter="formatter" label="状态" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('remark')" prop="remark" label="备注" />
        <!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('createUserId')" prop="createUserId" label="添加人id" />-->
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('createUserCode')" prop="createUserCode" label="添加人编码" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('createUserName')" prop="createUserName" label="添加人名称" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('createTime')" prop="createTime" label="添加时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <!--        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('updateUserId')" prop="updateUserId" label="修改人id" />-->
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('updateUserCode')" prop="updateUserCode" label="修改人编码" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('updateUserName')" prop="updateUserName" label="修改人名称" />
        <el-table-column :render-header="crud.renderHeader" v-if="columns.visible('updateTime')" prop="updateTime" label="修改时间">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.updateTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :render-header="crud.renderHeader" v-permission="['admin','printConfig:edit','printConfig:del']" label="操作" width="150px" align="center">
          <template slot-scope="scope">
            <udOperation
              :data="scope.row"
              :permission="permission"
            />
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudPrintConfig from '@/api/erp/printConfig'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import {add} from "../../../api/erp/printConfig";

// crud交由presenter持有
const defaultCrud = CRUD({ title: '打印模块配置', url: 'api/printConfig', sort: 'id,desc', crudMethod: { ...crudPrintConfig }})
const defaultForm = { id: null, code: null, name: null, type: null, templateId: null, templateName: null, deptId: null, deptCode: null, deptName: null, userId: null, userCode: null, userName: null, seq: null, status: null, remark: null, createUserId: null, createUserCode: null, createUserName: null, createTime: null, updateUserId: null, updateUserCode: null, updateUserName: null, updateTime: null }
export default {
  name: 'PrintConfig',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(defaultCrud), header(), form(defaultForm), crud()],
  dicts: ["t_print_config_code",'t_sys_order_code_model'],
  created() {
    this.switchStatus = this.form.status === 1
  },
  data() {
    return {
      showInit:false,
      initPrintData:{

      },
      initPrintQuery:{
        code:'',
        extra:[]
      },
      textarea:null,
      permission: {
        add: ['admin', 'printConfig:add'],
        edit: ['admin', 'printConfig:edit'],
        del: ['admin', 'printConfig:del'],
        import: ['admin', 'printConfig:import'],
        export: ['admin', 'printConfig:export'],
        copy: ['admin', 'printConfig:copy'],
        upload: ['admin', 'printConfig:upload'],
        download: ['admin', 'printConfig:download'],
        enable: ['admin', 'printConfig:enable'],
        disable: ['admin', 'printConfig:disable'],
      },
      rules: {
      },
      switchStatus:false,


    }
  },
  watch: {
  },
  methods: {
    test(){
      console.log(this.form)
    },
    // 获取数据前设置好接口地址
    [CRUD.HOOK.beforeRefresh]() {
      return true
    }, // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
    },
    updateDept(val){
      console.log(val)
      this.form.deptId = val.deptId
      this.form.deptName = val.deptName
    },
    printInitSubmit(){
      crudPrintConfig.init(this.initPrintQuery).then(res=>{
        this.textarea = res.data.content
      })
    },
    upStatus(s){
      this.form.status = s?1:0
    },
    formatter(row, column) {
      return row.status == 1?"启用":"禁用";
    },
    upCode(val){
      this.form.code = val.value
      this.form.name = val.label
    },
    save(){
      crudPrintConfig.add(this.form)
      this.crud.cancelCU()
      setTimeout(()=>{this.crud.refresh()},1000)
    },
    addextra(){
      this.initPrintQuery.extra.push({code:null,columnName:null})
    },
    removeextr(){
      this.initPrintQuery.code = null
      this.initPrintQuery.extra = []
    }

  }
}
</script>

<style scoped>
.table-img {
  display: inline-block;
  text-align: center;
  background: #ccc;
  color: #fff;
  white-space: nowrap;
  position: relative;
  overflow: hidden;
  vertical-align: middle;
  width: 32px;
  height: 32px;
  line-height: 32px;
}
</style>
